package com.example.action.user;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.model.user.UserInfo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 演示Swagger2 API
 * http://localhost:8080/swagger-ui.html
 * @author tongyufu
 *
 */
@RestController
@RequestMapping("/user")
public class UserAction {

    // 创建线程安全的Map 
    static Map<Long, UserInfo> users = Collections.synchronizedMap(new HashMap<Long, UserInfo>());

    @ApiOperation(value = "获取用户列表")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<UserInfo> getUserInfoList() {
        // 处理"/users/"的GET请求，用来获取用户列表 
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递 
        return new ArrayList<UserInfo>(users.values());
    }

    @ApiOperation(value = "新增用户", notes = "根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户信息实体", required = true, dataType = "UserInfo")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public String postUserInfo(@ModelAttribute UserInfo user) {
        // 处理"/users/"的POST请求，用来创建UserInfo 
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数 
        users.put(user.getId(), user);
        return "success";
    }

    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public UserInfo getUserInfo(@PathVariable Long id) {
        // 处理"/users/{id}"的GET请求，用来获取url中id值的UserInfo信息 
        // url中的id可通过@PathVariable绑定到函数的参数中 
        return users.get(id);
    }

    @ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Long"),
                         @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUserInfo(@PathVariable Long id, @ModelAttribute UserInfo user) {
        // 处理"/users/{id}"的PUT请求，用来更新UserInfo信息 
        UserInfo u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return "success";
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteUserInfo(@PathVariable Long id) {
        // 处理"/users/{id}"的DELETE请求，用来删除UserInfo 
        users.remove(id);
        return "success";
    }

}
